package com.yonyou.sto.modules.ims.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yonyou.sto.common.api.CommonResult;
import com.yonyou.sto.modules.ims.model.ImsInvoiceItem;
import com.yonyou.sto.modules.ims.service.ImsInvoiceItemService;
import com.yonyou.sto.modules.ums.model.UmsNcMategrade;
import com.yonyou.sto.modules.ums.service.UmsNcMategradeService;
import com.yonyou.sto.modules.ums.service.impl.ConfigNameServicImpl;
import com.yonyou.sto.modules.ums.util.ConfigNameUtil;
import com.yonyou.sto.modules.ums.util.NcMateGradeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author aegon
 * @since 2020-09-15
 */
@RestController
@RequestMapping("/server/ims/imsInvoiceItem")
@Api(tags = "ImsInvoiceItemController", description = "单据明细管理")
public class ImsInvoiceItemController {

    @Resource
    private ConfigNameServicImpl configNameServic;

    @Resource
    private ImsInvoiceItemService imsInvoiceItemService;

    @Resource
    private UmsNcMategradeService umsNcMategradeService;

    @ApiOperation("根据开票通知的ID查询开票明细列表")
    @GetMapping(value = "/getInfoByBillId")
    public CommonResult<List<ImsInvoiceItem>> getInfoByBillId(@RequestParam String billId) {
        List<ImsInvoiceItem> items = imsInvoiceItemService.listByBillId(billId);
        if (CollUtil.isEmpty(items)) return CommonResult.failed("未查询到数据");
        return CommonResult.success(items);
    }

    @ApiOperation("修改单据明细信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult update(@RequestBody ImsInvoiceItem invoiceItem) {
        return imsInvoiceItemService.replace(invoiceItem);
    }

    @ApiOperation("修改单据明细信息")
    @RequestMapping(value = "/updateMateName/{id}", method = RequestMethod.POST)
    public CommonResult updateMateName(@PathVariable String id,@RequestBody List<String > ids) {
        ImsInvoiceItem byId = imsInvoiceItemService.getById(id);

        //根据最新的客户物料名称配置出最新的客户物料名称，直接修改数据库。最后页面会刷新
        //如此一来，需要在配置客户物料名称中加入是否有客户物料名称的逻辑
        String pkNcmateg = byId.getPkNcmateg();
        UmsNcMategrade umsNcMategrade = umsNcMategradeService.getOne(new LambdaQueryWrapper<UmsNcMategrade>()
                .eq(UmsNcMategrade::getPkNc, pkNcmateg)
        );
        umsNcMategrade.setMatename(ids);
        NcMateGradeUtil.toOrigin(umsNcMategrade);
        NcMateGradeUtil.setMateName(umsNcMategrade);
        configNameServic.setumsNcMategrade(umsNcMategrade);
        ConfigNameUtil.getMateItem(umsNcMategrade, byId);
        boolean result = imsInvoiceItemService.updateById(byId);
        if (result){
            return CommonResult.success("修改开票名称成功");
        }else {
            return CommonResult.failed("修改开票名称失败");
        }
    }

    @ApiOperation("修改单据明细信息")
    @RequestMapping(value = "/updateSpec/{id}", method = RequestMethod.POST)
    public CommonResult updateSpec(@PathVariable String id,@RequestBody String name) {
        ImsInvoiceItem byId = imsInvoiceItemService.getById(id);
        if(" ".equals(name)){
            name = "";
        }
        byId.setProductSpec(name);
        boolean result = imsInvoiceItemService.updateById(byId);
        if (result){
            return CommonResult.success("修改型号成功");
        }else {
            return CommonResult.failed("修改型号失败");
        }
    }


    @ApiOperation("修改单据明细信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public CommonResult update(@PathVariable String id,@RequestBody String name) {
        ImsInvoiceItem byId = imsInvoiceItemService.getById(id);
        if(" ".equals(name)){
            name = "";
        }
        byId.setCustmatname(name);
        boolean result = imsInvoiceItemService.updateById(byId);
        if (result){
            return CommonResult.success("修改物料名称成功");
        }else {
            return CommonResult.failed("修改物料名称失败");
        }
    }

    @ApiOperation("合并单据明细信息")
    @RequestMapping(value = "/merge", method = RequestMethod.POST)
    public CommonResult merge(@RequestBody List<String> id) {
        return imsInvoiceItemService.merge(id);
    }

    @ApiOperation("新增单据明细单据明细信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult create(@RequestBody ImsInvoiceItem invoiceItem) {
        if (invoiceItem==null)return CommonResult.failed("新增失败");
        return imsInvoiceItemService.create(invoiceItem);
    }

    @ApiOperation("删除未开票单据明细信息")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestBody List<String> ids) {
        return imsInvoiceItemService.delete(ids);
    }

    @ApiOperation("删除待开票单据明细信息")
    @RequestMapping(value = "/deleteApply", method = RequestMethod.POST)
    public CommonResult deleteApply(Principal principal, @RequestBody List<String> ids) {
        return imsInvoiceItemService.deleteApply(principal.getName(),ids);
    }
}

